open(BUF,"chr.txt");
$num = 0;
$count = 50;
while($line=<BUF>)
{
        chomp($line);
        #print $line."\n";
        @chrarr = split(" ",$line);
        chomp($chrarr[0]);
	#print "$chrarr[0]\t$chr{$chrarr[0]}\n";
        $chr{$chrarr[0]} = $chrarr[2];
	#print "$chrarr[0]\t$chr{$chrarr[0]}\n";
	$chr_arr[$num] = $chrarr[0];
	$num++;
}
$canon = "canonical_final";
open(BUFF,"$canon");
#open(WRBU,">txtt");
$k =0;
$prevchr = "chr1";
$line=<BUFF>;
system("tail -1 $canon >lastline.txt");
open(KU,"lastline.txt");
$lastline = <KU>;
system("rm lastline.txt");
chomp($lastline);
$zzz = 2;
while($line=<BUFF> || $zzz ==1)
{
        chomp($line);
	#print $line."\n";
	@array = split(/\t/,$line);
	#if($line eq $lastline)
	#{
#		$zzz =1;
#	}
	if(($array[0] ne $prevchr || $zzz ==1)&& exists($chr{$prevchr})) 
	{
		@order = sort {$pos[$a] <=> $pos[$b]}  0 .. $#pos;

		for($i=0;$i<@order;$i++)
		{
		       $pos1[$i] = $pos[$order[$i]];
		       $whole1[$i] = $whole[$order[$i]];
			#print $pos1[$i]."\t".$whole1[$i]."\n";
		}
	
                $end = $chr{$prevchr};
                $prv = 1;
                $next = $prv+$count-1;
		$nu = 0;

		while($next < $end)
		{	
			$binname = $prevchr."_".$prv."_".$next;
			@value = split(/\t/,$whole1[$nu]);
			if($next < $pos1[$nu] )
			{
				#print WRBU $binname."\t"."intergenic "."\n";
				#$hasu{$binname} = "intergenic ";
				$prv = $prv+$count;
				$next = $prv+$count-1;
			}
			elsif($nu <@pos1 && $pos1[$nu] <= $next )
			{
				#print "wow $prv\t$next\t$pos1[$nu]\t$value[2]\n";
				if($value[2] <= $next)
				{
					$wholevalue = "";
					while($pos1[$nu] <= $next && $nu <@pos1 && $value[2] <= $next)
					{
					
						@value = split(/\t/,$whole1[$nu]);
					
						$wholevalue = $wholevalue."$value[0]";
						#$prev_gene_start = $pos1[$nu];
						#$prev_gene_stop =  $value[2];
						$nu++; 
						@value = split(/\t/,$whole1[$nu]);
					}
					#print  WRBU $binname."\t".$wholevalue."\n";
					if(exists($hasu{$binname}))
					{
					#	$hasu{$binname}=$hasu{$binname}."_"."$wholevalue";
					}
					else
					{
					#	$hasu{$binname} =$wholevalue;
					}
					#print $binname."\t".$hasu{$binname}."\n";
					print $binname."\t".$wholevalue."\n";
					$prv = $prv+$count;
					$next = $prv+$count-1;
				}
				else
				{		
					#print "sucess\n";
					$mn =0;
					$prev_bin_start[$mn] = $prv;	
					$prev_bin_stop[$mn] =  $next;
					$mn++;
					#print  WRBU $binname."\t".$value[0]."\n";
					while($value[2] >= $prv)
					{
						if(exists($hasu{$binname}))
                                        	{
                                          #      	$hasu{$binname}=$hasu{$binname}."_".$value[0];
                                        	}
                                        	else
                                        	{
                                           #     	$hasu{$binname} =$value[0];
                                        	}
						print $binname."\t".$value[0]."\n";
						
						$prv = $prv+$count;
						$next = $prv+$count-1;
						$prev_bin_start[$mn] = $prv;
						$prev_bin_stop[$mn] =  $next;
						$mn++;
						$binname = $prevchr."_".$prv."_".$next;
					}
					$nu++;
					@value = split(/\t/,$whole1[$nu]);
					#print "test $pos1[$nu] $prev_bin_stop\n"; 
					$mm=0;
					$mmm =0;
					#print "kk\n@prev_bin_start\n@prev_bin_stop\n";
					while($mm < $mn && $mmm == 0)
					{
					#	print "test $pos1[$nu] $prev_bin_stop[$mm]\n";
						if($pos1[$nu] <= $prev_bin_stop[$mm])
						{
							$prv = $prev_bin_start[$mm];
							$next = $prev_bin_stop[$mm];
							$mmm++;
						}
						$mm++;

					}		
					#$prv = $prv+$count;
					#$next = $prv+$count-1;
					
				
				}
			}
			else
			{
				#print WRBU $binname."\t"."intergenic end"."\n";
				#$hasu{$binname} = "intergenic end";
				$prv = $prv+$count;
				$next = $prv+$count-1;
			}	

		}		 	
	
		$k =0;
		@pos = ();
		@whole = ();
		@pos1 = ();
		@whole1 = ();
	}
	if(exists($chr{$array[0]}))
		{
			#$start = $array[4];
			#$stop = $array[5];
			#$pos[$k] = $start;
			#$whole[$k] = $array[0]."\t".$start."\t".$stop;
			#start
			if($array[9] eq 'n/a')
			{
				$pos[$k] = $array[1];
				$whole[$k] =  $array[3]."_noncoding_$array[1]"."_$array[2]"."\t".$array[1]."\t".$array[2];
				$k++;
			}
			else
			{
                        	@start = split(',',$array[9]);
                        	@stop = split(',',$array[10]);
                        	$prev_stop = "";
                        	for($i=0;$i<@stop;$i++)
                        	{
                                	if($prev_stop ne "")
                                	{
                                        	$pprev = $prev_stop+1;
                                        	$pos[$k] = $pprev;
                                        	$pstart = $start[$i]-1;
                                        	$whole[$k] = $array[3]."_intron_$pprev"."_$pstart"."\t".$pprev."\t".$pstart;
                                        	$k++;
                                	}
                                	$pos[$k] = $start[$i];
                                	$whole[$k] = $array[3]."_exon_$start[$i]"."_$stop[$i]"."\t".$start[$i]."\t".$stop[$i];
                                	$k++;
                                	$prev_stop = $stop[$i];
                        	}
			}
			#stop	
			#$k++;
		}
		else
		{
		}
		$prevchr = $array[0];
		 if($line eq $lastline)
        	{
                	$zzz =1;
        	}
		else
		{
			$zzz++;
		}
}
#foreach $key (sort keys %hasu) {
#     print WRBU "$key\t$hasu{$key}\n";
#}
